package com.carkeeper.shop.check.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.shop.check.service.StoreUserService;
import com.carkeeper.shop.pojo.Store;
import com.carkeeper.shop.pojo.StoreUser;
import com.carkeeper.shop.service.StoreService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 服务商主体
 */
@RequestMapping("/storeUser")
@RestController
@Api(tags = "主体用户")
@CrossOrigin
public class StoreUserController {
    @Autowired
    private StoreUserService storeUserService;
    @Autowired
    private StoreService storeService;

    /**
     * 已通过审核的服务商的展示
     * 展示已通过审核的服务商
     */
    @GetMapping("/findPage/{page}/{size}")
    @ApiOperation("查询列表")
    public PageResult findPage(@RequestParam Map searchMap, @PathVariable("page") String page, @PathVariable("size") String size) {
        Page<Store> storePage = storeUserService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult<Store>(storePage.getTotal(), storePage.getResult());
        return pageResult;
    }

    /**
     * 确认创建管理员
     */
    @PostMapping("/saveAdmin")
    @ApiOperation("创建管理员")
    public Result saveAdmin(@RequestBody StoreUser storeUser) {
        return storeUserService.saveAdmin(storeUser);
    }

    /**
     * 查看用户
     */
    @GetMapping("/userId/{page}/{size}")
    @ApiOperation("展示服务商用户")
    public PageResult userId(@RequestParam Map searchMap, @PathVariable("page") String page, @PathVariable("size") String size) {
        Page<StoreUser> storeUsers = storeUserService.userId(searchMap, page, size);
        PageResult pageResult = new PageResult(storeUsers.getTotal(), storeUsers.getResult());
        return pageResult;
    }

    /**
     * 重置用户密码
     */
    @PutMapping("/reset/{username}")
    @ApiOperation("重置服务商密码")
    public Result reset(@PathVariable("username") String username) {
        return storeUserService.reset(username);
    }

    /**
     * 锁定与解锁用户
     */
    @PutMapping("/lockUser/{username}")
    @ApiOperation("服务商账号锁定与解锁")
    public Result lockUser(@PathVariable("username") String username) {
        return storeUserService.lockUser(username);
    }

    @GetMapping("/createAdmin/{id}")
    @ApiOperation("显示创建管理员窗口的数据回显")
    public Map createAdmin(@PathVariable("id") String id) {
        return storeUserService.createAdmin(id);
    }
}
